package com.lineage.data.item_etcitem;

import com.lineage.config.ConfigOther;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_OwnCharStatus;
import com.lineage.server.serverpackets.S_PacketBoxGree;
import com.lineage.server.serverpackets.S_SPMR;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import com.lineage.william.logpcpower_new1;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Reactivating_Potion extends ItemExecutor {
  private static final Log _log = LogFactory.getLog(Reactivating_Potion.class);
  
  private int type1;
  
  public static ItemExecutor get() {
    return new Reactivating_Potion();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    if (pc.getLevel() < ConfigOther.logpclevel) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("等级太低以至于无法转生。"));
      return;
    } 
    if (pc.getMeteLevel() == this.type1) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("目前转生限制最高:" + this.type1 + "转"));
      return;
    } 
    pc.getInventory().removeItem(item, 1L);
    int pcObjid = pc.getId();
    int randomHp = pc.getMaxHp() * ConfigOther.logpcresthp / 100;
    int randomMp = pc.getMaxMp() * ConfigOther.logpcrestmp / 100;
    pc.setExp(1L);
    pc.onChangeExp();
    pc.resetLevel();
    if (ConfigOther.logpcpower) {
      pc.setBonusStats(0);
    } else {
      pc.setBonusStats(49);
    } 
    pc.setMeteLevel(pc.getMeteLevel() + 1);
    pc.resetBaseAc();
    pc.resetBaseMr();
    pc.resetBaseHitup();
    pc.resetBaseDmgup();
    pc.addBaseMaxHp((short)randomHp);
    pc.addBaseMaxMp((short)randomMp);
    pc.setCurrentHp(pc.getMaxHp());
    pc.setCurrentMp(pc.getMaxMp());
    pc.sendPacketsX8((ServerBasePacket)new S_SkillSound(pcObjid, 191));
    pc.resetMeteAbility();
    pc.sendPackets((ServerBasePacket)new S_OwnCharStatus(pc));
    pc.sendPackets((ServerBasePacket)new S_SPMR(pc));
    pc.sendPackets((ServerBasePacket)new S_ServerMessage(822));
    pc.setlogpcpower_SkillCount(pc.getlogpcpower_SkillCount() + ConfigOther.logpctfcount);
    logpcpower_new1.forIntensifyArmor(pc);
    L1Teleport.teleport(pc, 33441, 32809, (short)4, pc.getHeading(), true);
    if (ConfigOther.logpcallmsg)
      World.get().broadcastPacketToAll((ServerBasePacket)new S_ServerMessage("\\aH 恭喜玩家【" + pc.getName() + "】")); 
    World.get().broadcastPacketToAll((ServerBasePacket)new S_ServerMessage("\\aH 熬夜暴肝，转生第 " + pc.getMeteLevel() + " 次了！"));
    if (ConfigOther.logpcallmsg)
      World.get().broadcastPacketToAll((ServerBasePacket)new S_PacketBoxGree(
            "\\fV 恭喜玩家 " + pc.getName() + "\\fV  熬夜暴肝，转生第 " + pc.getMeteLevel() + "\\fV 次了！")); 
    try {
      pc.save();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this.type1 = Integer.parseInt(set[1]);
    } catch (Exception exception) {}
  }
}
